<!doctype html>



  


<html class="theme-next pisces use-motion">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />












  
  
  <link href="/vendors/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/vendors/font-awesome/css/font-awesome.min.css?v=4.4.0" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.0.1" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Android,Activity," />





  <link rel="alternate" href="/atom.xml" title="GrantonZhuang's Blog" type="application/atom+xml" />




  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.0.1" />






<meta name="description" content="IntentFilter 知识点总结。">
<meta property="og:type" content="article">
<meta property="og:title" content="IntentFilter 知识点总结">
<meta property="og:url" content="https://grantonzhuang.github.io/2016/07/30/IntentFilter/index.html">
<meta property="og:site_name" content="GrantonZhuang's Blog">
<meta property="og:description" content="IntentFilter 知识点总结。">
<meta property="og:updated_time" content="2016-07-30T12:48:51.270Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="IntentFilter 知识点总结">
<meta name="twitter:description" content="IntentFilter 知识点总结。">



<script type="text/javascript" id="hexo.configuration">
  var NexT = window.NexT || {};
  var CONFIG = {
    scheme: 'Pisces',
    sidebar: {"position":"left","display":"post"},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: 0,
      author: '博主'
    }
  };
</script>




  <link rel="canonical" href="https://grantonzhuang.github.io/2016/07/30/IntentFilter/"/>

  <title> IntentFilter 知识点总结 | GrantonZhuang's Blog </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  










  
  
    
  

  <div class="container one-collumn sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-meta ">
  

  <div class="custom-logo-site-title">
    <a href="/"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">GrantonZhuang's Blog</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle"></p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/index.html" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/index.html" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/index.html" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      

      
    </ul>
  

  
</nav>

 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  
  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                IntentFilter 知识点总结
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            <span class="post-meta-item-icon">
              <i class="fa fa-calendar-o"></i>
            </span>
            <span class="post-meta-item-text">发表于</span>
            <time itemprop="dateCreated" datetime="2016-07-30T20:45:23+08:00" content="2016-07-30">
              2016-07-30
            </time>
          </span>

          

          
            
          

          

          
          

          
        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>IntentFilter 知识点总结。</p>
<a id="more"></a>
<p>Intent 分为两种，显式 intent 和 隐式的 intent，显式 intent 指定了包名和类名，而隐式 intent 没有。</p>
<p>对于需要接受隐式 intent 的组件，我们需要指定过滤的规则，用 IntentFilter 对象来包裹过滤的信息。</p>
<p>IntentFilter 的过滤信息有 action, category, data</p>
<h3 id="action-的匹配规则"><a href="#action-的匹配规则" class="headerlink" title="action 的匹配规则"></a>action 的匹配规则</h3><p>一个过滤规则中可以含有多个 action，只要 <strong>intent 中的 action 的任意一个</strong> 和 <strong>过滤规则中的 action 的任意一个</strong> 匹配， 即为匹配成功。</p>
<h3 id="category-的匹配规则"><a href="#category-的匹配规则" class="headerlink" title="category 的匹配规则"></a>category 的匹配规则</h3><p>一个过滤规则中可以含有多个 category，要求 <strong>intent 中的所有 category</strong> 和 <strong>过滤规则中的 部分/所有 category</strong> 匹配， 即为匹配成功。</p>
<h3 id="data-的匹配规则"><a href="#data-的匹配规则" class="headerlink" title="data 的匹配规则"></a>data 的匹配规则</h3><p>与 action 类似，如果在过滤规则中定义了 action，那么 Intent 中必须也要有匹配的 data。</p>
<p>data 包含有以下的数据</p>
<ul>
<li>mimeType 媒体类型</li>
<li>URI<ul>
<li><strong>Scheme</strong> URI 的模式，这里我理解为协议</li>
<li><strong>Host</strong> 主机名</li>
<li><strong>Port</strong> 端口号</li>
<li><strong>Path</strong> 完整的路径</li>
<li><strong>PathPattern</strong> 使用正则表达式匹配路径</li>
<li><strong>PathPrefix</strong> 路径前缀</li>
</ul>
</li>
</ul>
<p>如</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">intent-filter</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">data</span> <span class="attr">android:mimeType</span>=<span class="string">"image/*"</span> /&gt;</span></div><div class="line">    <span class="comment">&lt;!-- more attrs --&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">intent-filter</span>&gt;</span></div></pre></td></tr></table></figure>
<p>为了匹配这个规则，我们这样子写</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">intent.serDataAndType(Uri.parse(<span class="string">"file:url"</span>), <span class="string">"image/png"</span>);</div></pre></td></tr></table></figure>
<p>另外，<code>setData()</code> 方法和 <code>setType()</code> 方法是互斥的，设置了 uri 会把 mimeType 清除，反之亦然。</p>
<p>要设置完整的 uri 和 mimeType，调用 <code>setDataAndType(Uri data, String type)</code> 方法.</p>
<h3 id="确保有组件可以收到隐式-intent-避免出错"><a href="#确保有组件可以收到隐式-intent-避免出错" class="headerlink" title="确保有组件可以收到隐式 intent 避免出错"></a>确保有组件可以收到隐式 intent 避免出错</h3><p>以 activity 为例， <code>PackageManager</code> 提供了以下方法， 这两个方法都返回 <strong>能够接收到这个隐式 intent 的 activity 集合或者信息。</strong></p>
<blockquote>
<p>其他的组件也有类似的方法。</p>
</blockquote>
<p>queryIntentActivities</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="comment">/*</span></div><div class="line"> * @see #MATCH_DEFAULT_ONLY</div><div class="line"> */</div><div class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">abstract</span> List&lt;ResolveInfo&gt; <span class="title">queryIntentActivities</span><span class="params">(Intent intent, <span class="keyword">int</span> flags)</span></span>;</div></pre></td></tr></table></figure>
<p>resolveActivity</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="comment">/**</span></div><div class="line"> * <span class="doctag">@see</span> #GET_RESOLVED_FILTER</div><div class="line"> */</div><div class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">abstract</span> ResolveInfo <span class="title">resolveActivity</span><span class="params">(Intent intent, <span class="keyword">int</span> flags)</span></span>;</div></pre></td></tr></table></figure>
<p>我们看常量 <code>MATCH_DEFAULT_ONLY</code> 的定义：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="comment">/**</span></div><div class="line"> * Resolution and querying flag: if set, only filters that support the</div><div class="line"> * &#123;<span class="doctag">@link</span> android.content.Intent#CATEGORY_DEFAULT&#125; will be considered for</div><div class="line"> * matching.  This is a synonym for including the CATEGORY_DEFAULT in your</div><div class="line"> * supplied Intent.</div><div class="line"> */</div><div class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> MATCH_DEFAULT_ONLY  = <span class="number">0x00010000</span>;</div></pre></td></tr></table></figure>
<p>也就是说，需要保证不出错，建议将 flags 参数设置为 <code>MATCH_DEFAULT_ONLY</code>，<br>以排除无法接收隐式 intent 的 activity （也就是 category 中不包含 <code>android.intent.category.DEFAULT</code> 的 activity ），<br>这样子，如果这个方法能返回不为空的数据，则 startActivity() 方法一定会调用成功。</p>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Android/" rel="tag">#Android</a>
          
            <a href="/tags/Activity/" rel="tag">#Activity</a>
          
        </div>
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2016/07/08/lifeCycleOfActivity/" rel="next" title="Activity 知识点">
                <i class="fa fa-chevron-left"></i> Activity 知识点
              </a>
            
          </div>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2016/08/04/SthAboutActivityLaunch/" rel="prev" title="Activity 的启动过程">
                Activity 的启动过程 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel ">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="http://ww1.sinaimg.cn/mw690/6e8939a6gw1f3g1u1mc6lj20hs0hsmxf.jpg"
               alt="Granton Zhuang" />
          <p class="site-author-name" itemprop="name">Granton Zhuang</p>
          <p class="site-description motion-element" itemprop="description"></p>
        </div>
        <nav class="site-state motion-element">
          <div class="site-state-item site-state-posts">
            <a href="/archives/index.html">
              <span class="site-state-item-count">26</span>
              <span class="site-state-item-name">日志</span>
            </a>
          </div>

          

          
            <div class="site-state-item site-state-tags">
              <a href="/tags/index.html">
                <span class="site-state-item-count">11</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        
          <div class="feed-link motion-element">
            <a href="/atom.xml" rel="alternate">
              <i class="fa fa-rss"></i>
              RSS
            </a>
          </div>
        

        <div class="links-of-author motion-element">
          
        </div>

        
        

        
        

      </section>

      
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">
            
              
            
            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#action-的匹配规则"><span class="nav-number">1.</span> <span class="nav-text">action 的匹配规则</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#category-的匹配规则"><span class="nav-number">2.</span> <span class="nav-text">category 的匹配规则</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#data-的匹配规则"><span class="nav-number">3.</span> <span class="nav-text">data 的匹配规则</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#确保有组件可以收到隐式-intent-避免出错"><span class="nav-number">4.</span> <span class="nav-text">确保有组件可以收到隐式 intent 避免出错</span></a></li></ol></div>
            
          </div>
        </section>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2016</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Granton Zhuang</span>
</div>

<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Pisces
  </a>
</div>

        

        
      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  



  
  <script type="text/javascript" src="/vendors/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/vendors/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/vendors/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/vendors/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/vendors/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/vendors/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.0.1"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.0.1"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.0.1"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.0.1"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.0.1"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.0.1"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.0.1"></script>



  



  




  
  

  

  

  

</body>
</html>
